package com.twang.stock.service.jinyan;

import com.twang.stock.entity.CompanyCodeInfo;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyCodeInfoService {

    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;

    private static Map<String,CompanyCodeInfo> stockMap=new HashMap<>();

    public CompanyCodeInfo getInfoByStockName(String stockName){
        CompanyCodeInfo companyCodeInfo=stockMap.get(stockName);
        if(null== companyCodeInfo) {
            Example example = new Example(CompanyCodeInfo.class);
            example.createCriteria().andLike("companyCode", "%" + stockName + "%");
            List<CompanyCodeInfo> list = companyCodeInfoMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(list)) {
                return null;
            }
            companyCodeInfo = list.get(0);
            stockMap.put(stockName,companyCodeInfo);
        }
        return companyCodeInfo ;
    }

}
